package com.gitee.wsl.time.schedule.ext

import com.gitee.wsl.time.schedule.TimeScheduler
import kotlinx.coroutines.delay
import kotlin.time.ExperimentalTime
import kotlin.time.Instant


/**
 * Will execute [block] while it will return true as a result of its calculation
 */
@OptIn(ExperimentalTime::class)
suspend inline fun TimeScheduler.doWhile(block: (Instant) -> Boolean) {
    var latest: Instant? = null
    do {
        delay(1L)
        val result = doOnce {
            if (latest != it) {
                latest = it
                block(it)
            } else {
                null
            }
        }
    } while (result == null || result)
}